package com.hzit.search.config;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import lombok.Data;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.RestHighLevelClientBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@Configuration
@ConfigurationProperties(prefix = "elasticsearch")
@Data
public class ElasticsearchConfig {

	private String host;
	private int port;

	//依赖ElasticsearchClient来访问elasticsearch索引库：
	@Bean
	public ElasticsearchClient  elasticsearchClient(){
		RestClient restClient = RestClient.builder(new HttpHost(host,port)).build();
		ElasticsearchTransport transport = new RestClientTransport(
				restClient, new JacksonJsonpMapper());
		return new ElasticsearchClient(transport);
	}

	@Bean
	public RestHighLevelClient highLevelClient(){
		RestClientBuilder builder = RestClient.builder(new HttpHost(host, port));
		return new RestHighLevelClient(builder);
	}
}
